class GamesController < ApplicationController
  
  before_filter :load_fset, :except => [:destroy]
  before_filter :load_game, :only => [:play, :next_card, :show, :update, :destroy, :correct, :wrong, :just_wrong, :until_correct]
  before_filter :game_over, :only => [:next_card, :play]
  before_filter :check_logged_in #, :only => [:destroy, :update, :create, :edit, :new]
   
  def load_game
    @game = Game.find(params[:id])
	@score = @game.score
  end 

  def load_fset
    @fset = Fset.find(params[:fset_id])
  end	
  #
  # This short circuts the play page if the game has been finished. 
  #
  # TODO - i need to add a "game over" page that says the score or whatever 
  # and then have it redirect to there. -colleen 
  def game_over
	# if there is no cardnum then you're not in the middle of a game - start OVER!
    if session[:cardnum].equal?(nil) #or !session[:game_id].equal?(params[:id])
		session[:shuffled_cards] = @game.get_cards(session[:game_format])
 		session[:game_id] = params[:id]
		# put a message in the flash for why the game is over before it started.
		if Game.game_over?(session[:shuffled_cards])
			flash[:notice] = 'You don\'t have any wrong ones !!'
		end
	end 
#	if !(session[:cardnum].equal?(nil)) and @game.game_over?(session[:shuffled_cards])
	if Game.game_over?(session[:shuffled_cards])
		# reset the card num so that it is clear that we're not in the middle of a game
		session[:cardnum] = nil 
		session[:game_format] = 1
 		session[:game_id] = nil
		redirect_to(stats_fset_path(@fset)) 
	end 
  end 
  
  # GET /games
  # GET /games.xml
  def index
    @games = Game.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @games }
    end
  end

  
  # GET /games/1/play
  # GET /games/1/play.xml
  def next_card
  #TODO - right now this procedure does NOTHING - BUT it could do this stuff
  # instead of it being done in play so that you could refresh on the play page 
  # without messing stuff up
  #	session[:cardnum] = session[:shuffled_cards].shift	
#	@flashcard_number = session[:cardnum] #TODO  - not using this - right?
#	@flashcard  = session[:cardnum]
	redirect_to(play_fset_game_path(@game.fset, @game)) 
  end
  
  # GET /games/1/play
  # GET /games/1/play.xml
  def play
    next_card_number = session[:shuffled_cards].shift	
	session[:cardnum] = Flashcard.find_by_id(next_card_number)
	@flashcard_number = session[:cardnum] #TODO  - not using this - right?
	@flashcard  = session[:cardnum]
	
	cards_remaining = session[:shuffled_cards].length + 1
	cards_done = @game.wrong_count + @game.score
	@progress = 100 * cards_done / (cards_done + cards_remaining)
    respond_to do |format|
      format.html # play.html.erb
      format.xml  { render :xml => @game }
    end
  end

  
  # GET /games/1/play
  # GET /games/1/play.xml
  def show
	@games = @fset.get_stats (current_user)
	@num_stats = @games.length
#	@card_stats = CardScore.find(:all,  :conditions => ["user_id = ? and fset_id = ?", @game.user_id, @game.fset_id])
	@card_stats = CardScore.find(:all, 	
									:joins => :flashcard, 
									:select => "card_scores.flashcard_id, card_scores.times_correct, card_scores.times_incorrect",
									:conditions => ["flashcards.fset_id = ? ", @fset.id])	
    @num_card_stats = @card_stats.length
	@graph_info = Game.prepare_graph(@games)
	@graph_info_cards = CardScore.prepare_graph(@card_stats)
	respond_to do |format|
      format.html # play.html.erb
      format.xml  { render :xml => @game }
    end
  end

  
  # GET /games/new
  # GET /games/new.xml
  def new
    @game = Game.new
    @game.fset = @fset
	@game.user_id = current_user.id
	@game.score = 0
	@game.wrong_count = 0
    respond_to do |format|
      if @game.save
		format.html { redirect_to(next_card_fset_game_path(@game.fset, @game)) }
        format.xml  { render :xml => @game, :status => :created, :location => @game }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end

  # GET /games/1/edit
  def edit
    @game = Game.find(params[:id])
  end

  # POST /games
  # POST /games.xml
  def create
    @game = Game.new(params[:game])
    @game.fset = @fset
    respond_to do |format|
      if @game.save
		format.html { redirect_to(fset_game_path(@game.fset, @game)) }
        format.xml  { render :xml => @game, :status => :created, :location => @game }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /games/1
  # PUT /games/1.xml
  def update
    @game.fset = @fset
    respond_to do |format|
      if @game.update_attributes(params[:game])
        flash[:notice] = 'Game was successfully updated.'
        format.html { redirect_to(fset_game_path(@game.fset, @game)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end


  
  # PUT /games/1
  # PUT /games/1.xml
  # TODO - we're not really taking any user input so if it doesn't 
  # save we should do something other than barf on the user.
  def correct
	@game.score_card(session[:cardnum], true)
    respond_to do |format|
      if @game.save
        flash[:notice] = 'Great Job!'
        format.html { redirect_to(next_card_fset_game_path(@game.fset, @game)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end
  
    # PUT /games/1
  # PUT /games/1.xml
  # TODO - we're not really taking any user input so if it doesn't 
  # save we should do something other than barf on the user.
  def wrong
	@game.score_card(session[:cardnum], false)
	if session[:game_format] == 2 
		session[:shuffled_cards].push(session[:cardnum])	
	end
    respond_to do |format|
      if @game.save
        flash[:notice] = 'Maybe next time.'
        format.html { redirect_to(next_card_fset_game_path(@game.fset, @game)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  
  
  # DELETE /games/1
  # DELETE /games/1.xml
  def destroy
    @game.destroy

    respond_to do |format|
      format.html { redirect_to(games_url) }
      format.xml  { head :ok }
    end
  end
end
